package com.yxd.bdmapdemo.utils;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dong on 2017/5/14.
 */

public class PermissionUtils {

    private static VersionLowMListener mListener;
    private static String mDeniedMsg;


    /**
     * @param context    上下文
     * @param permission 权限
     * @param deniedMsg  拒绝权限后 再次请求的提示信息
     * @param listener   获取权限后的监听操作
     */
    public static void checkPer(Activity context, String permission, String deniedMsg, VersionLowMListener listener) {
        mDeniedMsg = deniedMsg;
        mListener = listener;
        L.d("HelloBdMapActivity#checkPer");
        //版本判断
        if (Build.VERSION.SDK_INT >= 23) {
            //减少是否拥有权限
            int checkCallPhonePermission = ContextCompat.checkSelfPermission(context, permission);
            if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
                //请求权限
                L.d("HelloBdMapActivity#>= 23");
                ActivityCompat.requestPermissions(context, new String[]{permission}, 100);
                return;
            }
        }

        if (mListener != null) {
            L.d("HelloBdMapActivity#versionLowMAction");
            mListener.versionLowMAction();
        }

    }

    public static void showDialog(final Context context, String msg) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage(msg);
        builder.setTitle("提示");
        builder.setPositiveButton("去设置", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                openSetPermission(context);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

 /*   public void setVersionLowMAction(VersionLowMListener listener) {
        mListener = listener;
    }*/

    public static void requestPermissionResult(Context context, String[] permissions) {

        List<String> deniedPermissions = getDeniedPermissions(context, permissions);
        if (deniedPermissions.size() == 0) {
            L.d("权限用户都同意授予");
            // 权限用户都同意授予了
            if (mListener != null) {
                mListener.versionLowMAction();
            }
        } else {
            // 你申请的权限中 有用户不同意的
            L.d("有用户不同意的权限");
            showDialog(context, mDeniedMsg);
        }
    }


    /**
     * 获取没有授予的权限
     *
     * @param context Activity or Fragment
     * @return 没有授予过得权限
     */
    public static List<String> getDeniedPermissions(Context context, String[] requestPermissions) {
        List<String> deniedPermissions = new ArrayList<>();
        for (String requestPermission : requestPermissions) {
            // 把没有授予过的权限加入到集合
            if (ContextCompat.checkSelfPermission(context, requestPermission)
                    != PackageManager.PERMISSION_GRANTED) {
                deniedPermissions.add(requestPermission);
            }
        }
        return deniedPermissions;
    }


    /**
     * 打开系统设置权限界面
     *
     * @param context
     */
    public static void openSetPermission(Context context) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
        }
        context.startActivity(localIntent);
    }

    /**
     * 检测所有权限是否授权
     *
     * @param context
     */
    public static void isPermission(Context context) {
        PackageManager pm = context.getPackageManager();
        String packageName = context.getPackageName();
        PackageInfo pi;
        try {
            // 参数2必须是PackageManager.GET_PERMISSIONS
            pi = pm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
            String[] permissions = pi.requestedPermissions;
            if (permissions != null) {
                for (String str : permissions) {
                    boolean isPer = (PackageManager.PERMISSION_GRANTED ==
                            pm.checkPermission(str, packageName));
                    L.e(isPer + "\t" + str);
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取Context
     *
     * @param object
     * @return
     */
    public static Activity getActivity(Object object) {
        if (object instanceof Activity) {
            return (Activity) object;
        }
        if (object instanceof Fragment) {
            return ((Fragment) object).getActivity();
        }
        return null;
    }

    public interface VersionLowMListener {
        void versionLowMAction();

    }

}
